﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Applay
{
    public class Wrapper
    {
        private readonly ServiceFilterCollection filters;
        
        internal Func<IPrincipal> getPrincipal;
        
        public Wrapper()
        {
            this.filters = new ServiceFilterCollection();
        }

        public ValidationScope ValidationScope { get; internal set; }

        public T Wrap<T>(T service) where T : Service
        {
            Contract.Requires(service != null);

            return (T)new Proxy(this, service).GetTransparentProxy();
        }

        public ServiceFilterCollection Filters
        {
            get { return this.filters; }
        }

        public void EnableAuthorization(Func<IPrincipal> getPrincipal)
        {
            this.getPrincipal = getPrincipal;
        }

        public ValidationScope BeginValidation()
        {
            return this.BeginValidation(new ValidationState());
        }

        public ValidationScope BeginValidation(IValidationState validationState)
        {
            Contract.Requires(validationState != null);

            return new ValidationScope(this, validationState);
        }
    }
}
